Exception when adding something to an array

Hello,

my code always throws and exception when I try to run it. I try to count the values of one attribute within a module. I store every value in a two-dimensional array. x = value, y = number of occurrences. However when I try to update the y value in my array my code crashes. Seems to be this loc

put(attribArray, z , arrayPosition(s, attribArray)  ,1) {code)
 
Has someone maybe a hint? The DOORS log is not really helpful ;-)
 
Here is my complete code:
 
Thanks in advance!

Object obj = null
int count = 0
int z = 0
Array attribArray = create(1,1)
string s = ""

// get position of the value within the array
int arrayPosition(string attrib, Array attribArray)
{
int r = 0
int i = 0
for (i = 0; i <= sizeof(attribArray); i++)
{
if (attrib == (string get(attribArray, i, 0)))
{
r = i
break
}

}
return r
}

// check if value is already in the array
bool inArray(string attrib, Array attribArray)
{
bool r = false
int i = 0

for (i = 0; i <= sizeof(attribArray); i++)
{
if (attrib == (string get(attribArray, i, 0)))
{
r = true
break
}
}
return r
}
for obj in current Module do
{
s = obj."_some_attribute"

if ( s != "")
{

if ( inArray(s, attribArray) == false )
{
put(attribArray, s, count, 0)
put(attribArray, 0, count, 1)
count++

}
else if( inArray(s, attribArray) == false)

{

z = (int get(attribArray, arrayPosition(s, attribArray) , 1))
z++
put(attribArray, z , arrayPosition(s, attribArray) ,1)

}
}
}{code}


SystemAdmin - Tue Mar 26 04:51:47 EDT 2013

Re: Exception when adding something to an array
SystemAdmin - Tue Mar 26 04:54:06 EDT 2013

Sorry for that messy format. The forum software ignored my {code} tags.

Re: Exception when adding something to an array
Tony_Goodman - Tue Mar 26 05:14:22 EDT 2013

I haven't looked at your code, but if I understand correctly then I think it might be better to use a skip list.
Here is an example that lists attribute values in a dialog.

pragma runLim,0
 
 
DB db = null
DBE dbeResults = null
DBE dbeAttrs = null
 
string dummy[] = { }
Skip skipAttributes = createString
 
Module m = current Module
 
/**
    getAttributes
**/
void getAttributes()
{
        AttrDef ad = null
        string attr = ""
        int index = 0
        
        for ad in m do
        {
                if (!ad.object) continue
                        
                put(skipAttributes, ad.name, ad.name)
        }
        
        for attr in skipAttributes do
        {
                insert(dbeAttrs, index, attr, iconNone)
                index++
        }
        
        set(dbeAttrs, 0, true)
}
 
 
/**
        doList
**/
void doList(DB db)
{
        Buffer resBuf = create
 
        string attr = get(dbeAttrs)
 
        Module m = current
        Object o = null
        int count = 0
 
        Skip attrValues = createString
        string s = ""
 
        for o in m do
        {
                s = o.attr""
        
                if (find(attrValues, s, count))
                {
                        delete(attrValues, s)
                        put(attrValues, s, count + 1)
                }
                else
                {
                        put(attrValues, s, 1)
                }
 
        }
 
        for count in attrValues do
        {
                resBuf += "\"" (string key attrValues)  "\" (" count ")\n"
        }
 
        delete(attrValues)
        attrValues = null
 
        set(dbeResults, tempStringOf(resBuf))
 
        delete(resBuf)
        resBuf = null
}
 
/**
        buildDialog
**/
void buildDialog()
{
        db = create(current Module, "List Attribute Values")
        
        label(db, "This utility lists all the different values found for the given attribute.\n")
        dbeAttrs = listView(db, 0, 190, 15, dummy)
        
        dbeResults = text(db, "Results", "", 60, 150, false)
        
        apply(db,"OK", doList)
        
        realize(db)
        
        insertColumn(dbeAttrs, 0, "", 160, iconNone)
        getAttributes()
        
        show(db)
}
 
/**
        listAttributeValues
**/
void listAttributeValues()
{
        m = current Module
        
        buildDialog()
}
 
listAttributeValues()

 


Tony Goodman, www.smartdxl.com

 

Re: Exception when adding something to an array
SystemAdmin - Tue Mar 26 08:17:01 EDT 2013

Excellent! That's much easier than my first idea. Thanks a lot!

Re: Exception when adding something to an array
llandale - Tue Mar 26 13:05:56 EDT 2013

  • The first position in the Array is zero "0" not one "1".
  • The problem isn't the "put", its the "get" inside the "put".


Two things:

 

  1. If the "sizeof" array is "1" then the last valid row is "zero". Thus, your "i" loop should be this; changing "<=" to "<"
    • for (i = 0; i < sizeof(attribArray); i++)
  2. do this:
    • int r = -1


Seems to me you are searching the array 3 times for every duplicate entry. Instead, delete function inArray(), call initially call arrayPosition(), and when position is -1 then insert it into the Array.

I think it better to have a global variable "int CurrentSize" and use that instead of sizeof().

 

 

 

  • Louie


I wrote this many years ago, I think it is just what you want:

 

 

 

//*********************
bool    fPutSkipIncrement(Skip skpList, string KeyValue)
{     // Insert the KeyValue into the Skip list's 'Key'.
        // The 'Data' portion of the Skip is set to 1 for new inserts and is
        //    incremented for already existing 'Key's in the Skip.
        // 'Data' MUST be of type Integer.
        //   (represents the number of references).
        // Return whether this is a first insert for KeyValue into the Skip.
 
        bool    IsNewValue = false
 
        int     NumReferences = 0       // Remains zero if next "find" doesn't find anything:
        if(find(skpList, KeyValue, NumReferences))
             delete(skpList, KeyValue)          // Remove It
        else IsNewValue = true                  // Mark it New
                        // ReInsert new or updated counts
        NumReferences++
        put (skpList, KeyValue, NumReferences)
        return(IsNewValue)
}     // end fPutSkipIncrement()
// Sample
Skip skp = create() // KEY: 'string' Values; DATA: 'int' Count Found
// Find
for whatever
{  find Value
   fPutSkipIncrement(skp, Value)
}
// Report
int Count
for Count in skp do
{  Value = (string key skp)
   print Count "\t# found for: " Value "\n"
}